package com.summer.controller.plateformChannel;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.entity.PlateformChannel;
import com.summer.mapper.PlateformChannelMapper;
import com.summer.service.IPlateformChannelService;
import com.summer.util.CallBackResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 渠道商Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/platformChannel")
public class PlateformChannelController {

    @Resource
    private IPlateformChannelService plateformChannelService;

    @Resource
    private PlateformChannelMapper plateformChannelMapper;

    /**
     * 分页查询渠道列表
     * @param jsonData
     * @return
     */
    @PostMapping("/query")
    public String query(@RequestBody String jsonData) {
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        log.info("【查看渠道列表】查看渠道分页列表，params={}" + params.toString());
        //开启分页
        PageHelper.startPage(params);
        PageInfo<PlateformChannel> pageInfo = plateformChannelService.query(params);
        return CallBackResult.returnJson(pageInfo);
    }

    /**
     * 更新渠道状态
     * @param jsonData
     * @return
     * @throws Exception
     */
    @PostMapping("/updateByStatus")
    public String updateByStatus(@RequestBody String jsonData) throws Exception {
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        log.info("【更新渠道状态】更新渠道状态，params={}" + params.toString());
        PlateformChannel plateformChannel = checkId(params);
        if (null == plateformChannel)
            return CallBackResult.returnJson(CallBackResult.BUSINESS_ERROR, "参数非法");
        PlateformChannel newOrder = new PlateformChannel();
        newOrder.setId(plateformChannel.getId());
        newOrder.setStatus((Integer) params.get("status"));
        try {
            plateformChannelService.updateByStatus(newOrder);
            return CallBackResult.returnJson("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return CallBackResult.returnJson("操作失败");
        }
    }

    /**
     * 添加渠道
     * @param jsonData
     * @return
     * @throws Exception
     */
    @PostMapping("/createChannel")
    public String createChannel(@RequestBody String jsonData) throws Exception {
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        log.info("【添加渠道】 createChannel params=" + params.toString());
        try {
            plateformChannelService.insertChannel(params);
            return CallBackResult.returnJson("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CallBackResult.returnJson("操作失败");
        }
    }

    /**
     * 根据请求id查询渠道
     * @param params
     * @return
     */
    private PlateformChannel checkId(Map<String, Object> params) {
        Object idObj = params.get("id");
        if (null == idObj) {
            return null;
        }
        return plateformChannelMapper.selectByPrimaryKey(Integer.parseInt(idObj.toString()));
    }
}
